package com.amazon.shop.util;


import com.alibaba.fastjson.JSONObject;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.bean.vo.Ret;

/**
 * 创建时间：13:56 2021/1/13
 *
 * @author lzj
 * @since JDK 1.8.0_171
 * 类说明：
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 */
public class ResultUtil {

    public static Ret success(){
        return success(ResultEnum.SUCCESS,null);
    }

    public static Ret success(Object data){
        return success(ResultEnum.SUCCESS,data);
    }

    public static Ret success(ResultEnum result,Object data){
        return success(result.getCode(),result.getServecode(),result.getMsg(),data);
    }

    public static Ret success(String code,String servcode,String msg,Object data){
        Ret ret = new Ret();
        ret.setCode(code);
        ret.setServecode(servcode);
        ret.setMsg(msg);
        ret.setData(data);
        return ret;
    }

    public static Ret error(){
        return success(ResultEnum.FAILURE,null);
    }

    public static Ret error(ResultEnum resultEnum){
        return success(resultEnum,null);
    }

    public static Ret error(String code,String servcode,String msg){
        return success(code,servcode,msg,null);
    }

    /**
     * 接口跨域实现返回
     * @param callback 前端传递的回调方法名
     * @param obj 返回的结果数据
     * @return
     */
    public static String callback(String callback,Object obj){
        String result = callback + "(" + JSONObject.toJSONString(obj) + ")";
        return result;
    }
}
